package cn.com.cennavi.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

/**
 * didi生成的分车道路况对应的车辆和时间查找
 * 
 * @author Thinkpad
 *
 */
public class StandardDidiTimeComputer {
	private static String didiFile = "D:\\data\\分车道校验\\20160905.log";
	private static SimpleDateFormat sf = new SimpleDateFormat("HHmmss");
	private static SimpleDateFormat mf = new SimpleDateFormat("HHmm");
	public static void main(String[] args) throws IOException, ParseException {
		// TODO Auto-generated method stub
//		Date dd = new Date(Long.parseLong("1467252300") * 1000l);
//		String timeInt = sf.format(dd);
//		System.out.println(timeInt);
		GetTimeCarId(didiFile);
	}

	/**
	 * 计算分车道路况对应的车辆和时间
	 * 
	 * @param didiFile2
	 * @throws IOException
	 * @throws ParseException 
	 */
	private static void GetTimeCarId(String didiFile2) throws IOException, ParseException {
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new FileReader(new File(didiFile2)));
		String line = "";
		HashSet<String> nodeDir = new HashSet<String>();
		HashMap<String, TreeMap<Long, TreeMap<Integer, Integer>>> carIdMap = new HashMap<String, TreeMap<Long,TreeMap<Integer,Integer>>>();
		while ((line = br.readLine()) != null) {

			HashSet<String> carIDSet = new HashSet<String>();
			HashSet<String> timeSet = new HashSet<String>();
			String[] lineStr = line.split(",");
//			System.out.println("当前时间戳：" + lineStr[8]);
//			System.out.println("当前路口id：" + lineStr[0]);
//			System.out.println("当前时间戳：" + line);
			Long nowTime = Long.parseLong(lineStr[8]);
			String[] gpsInfo = lineStr[lineStr.length - 1].split("\\|");
			for (String ss : gpsInfo) {
				String[] sad = ss.split("\\#");
				for (String sss : sad) {
					String carId = sss.split("\\:")[0];
					String key=carId+"_"+lineStr[0];
					String time = sss.split("\\:")[1];
					Date dd = new Date(Long.parseLong(time) * 1000l);
					String timeInt = sf.format(dd);
					int timeInts = Integer.parseInt(timeInt);
					TreeMap<Long, TreeMap<Integer, Integer>> treeMap = carIdMap.get(key);
					if(treeMap!=null){
						 TreeMap<Integer, Integer>t=treeMap.get(nowTime);
						 if(t!=null){
							 t.put(timeInts, timeInts);
						 }else{
							 t=new TreeMap<Integer, Integer>();
							 t.put(timeInts, timeInts);
							 treeMap.put(nowTime, t);
						 }
						 
					}else{
						TreeMap<Integer, Integer>tt=new TreeMap<Integer, Integer>();
						tt.put(timeInts, timeInts);
						treeMap=new TreeMap<Long, TreeMap<Integer,Integer>>();
						treeMap.put(nowTime, tt);
						carIdMap.put(key, treeMap);
					}
					carIDSet.add(key);
					timeSet.add(timeInt);
				}
			}

//			String timeStr = "";
//			String carIDStr = "";
//			for (String carIds : carIDSet) {
//				carIDStr += carIds + "|";
//			}
//			for (String times : timeSet) {
//				timeStr += times + "|";
//			}
//			System.out.println(carIDStr);
//			System.out.println(timeStr);
//			System.out.println("-----------------------------------");
		}
		for (Entry<String, TreeMap<Long, TreeMap<Integer, Integer>>> e : carIdMap.entrySet()) {
			String carId = e.getKey();
			
			TreeMap<Long, TreeMap<Integer, Integer>> s =e.getValue();
//			if(s.size()>=5)continue;
//			System.out.println(carId);
			for(Map.Entry<Long, TreeMap<Integer, Integer>>sb:s.entrySet()){
				Long noeTime=sb.getKey();
				String timeStr=sb.getValue().keySet().toString().replace("[", "").replace("]", "").trim();
//				System.out.println(noeTime);
				String []timeS=timeStr.split(",");
				int num=timeS.length;
//				System.out.println(timeStr);
//				System.out.println((Integer.parseInt(timeS[num-1].trim())-Integer.parseInt(timeS[0].trim()))/60);
				Date date1=mf.parse((noeTime+"").substring(8, 12));
				Date date2=sf.parse(timeS[num-1].trim());
				long timeDiF=(date2.getTime()-date1.getTime())/(60*1000);
				System.out.println(timeDiF);
			}
//			System.out.println("-----------------------------------");
		}
	}

}
